package com.zsrt.Utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import org.junit.Test;

/**
 * 生成1个6位验证码
 * 规定0 o 1 l w v z 2不能出现
 * @author Administrator
 *
 */
public class CheckCodeUtil {
	public static String generateCode(){
		String[] arrays=new String[]{"3","4","5","6","7","8","9","a","b",
				"c","d","e","f","g","h","i","j",
				"k","l","m","n","x","y"};
		Random r=new Random();
	    StringBuffer sb=new StringBuffer();
	    for(int i=0;i<6;i++){
	    	int a=r.nextInt(arrays.length);
	    	sb.append(arrays[a]);
	    }
		return sb.toString();
	}
	@Test
     public void xx(){
		for(int i=0;i<20;i++){
    	  System.out.println(generateCode());}
     }
	public static BufferedImage getPhotoCode(String checkCode){
		Random random=new Random();
		BufferedImage bi=new BufferedImage(60, 25, BufferedImage.TYPE_INT_RGB);
		Graphics2D g=bi.createGraphics();
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
		Color c=new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
		g.setColor(c);
		g.fillRect(0, 0, 60, 25);
		g.setColor(new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue()));
		g.drawString(checkCode, 5, 20);
		for(int i=0;i<random.nextInt(100);i++){
			g.drawRect(random.nextInt(100), random.nextInt(30), 1, 1);
		}
		return bi;
	}
}
